//DONE

public class Sudoku
{
   public static String valid(String[] x)
   {
      /*
      String[] x = {
            "615429387",
            "472853169",
            "398176245",
            "746231598",
            "521984673",
            "983765412",
            "867542931",
            "154397826",
            "239618754"
            };*/
      /*String[] x = {
            "615---387",
            "472853169",
            "39817624-",
            "7-6231598",
            "5-1984673",
            "9-3765412",
            "867542931",
            "1-4397826",
            "239618754"
            };
      */
      
      
      int rows[] = {0,0,0,0,0,0,0,0,0};
      int columns[] = {0,0,0,0,0,0,0,0,0};
      int regions[] = {0,0,0,0,0,0,0,0,0};

      boolean row_result = false;
      boolean col_result = false;
      boolean region_result = false;
      boolean hiphen = false;
      
      int factor2 = 0;
      //Check regions
      for(int k=0;k<3;k++)
      {
         if(region_result)
            break;
         
         int factor1 = 0;
         for(int j=0;j<3;j++)
         {
            if(region_result)
               break;
            
            for(int i=0;i<9;i++)
            {             
               //Get region coordinate
               int index = i%3 + factor1;
               int row = i/3 + factor2;
               
               //Extract region character
               char regioncharacter = x[row].charAt(index);
               int regionindex;
               
               if(regioncharacter == '-')
               {
                  hiphen = true;
               }
               else
               {
                  regionindex = Integer.parseInt(String.valueOf(regioncharacter));
                  if(regions[regionindex-1] == 1)
                  {
                     region_result = true;
                     break;
                  }
                  else
                  {
                     regions[regionindex-1] = 1;
                  }
                   
               }
                                         
            }
            for (int q=0;q<9;q++)
            {
               regions[q] = 0;
            }
            factor1 = factor1+3;
         }
         factor2+=3;
      }
      
      //Check rows and columns
      for(int j=0;j<9;j++)
      {
         if(row_result || col_result)
            break;
         
         for(int i=0;i<9;i++)
         {
            //Rows
            char rowcharacter = x[j].charAt(i);
            int rowindex;
            if (rowcharacter == '-')
            {
               hiphen = true;
            }
            else
            {
               rowindex = Integer.parseInt(String.valueOf(rowcharacter));
               if(rows[rowindex-1] == 1)
               {
                  row_result = true;
                  break;
               }
               else
               {
                  rows[rowindex-1] = 1;
               }
            }
            
            //Columns
            char colcharacter = x[i].charAt(j);
            int colindex;
            if (colcharacter == '-')
            {
               hiphen = true;
            }
            else
            {
               colindex = Integer.parseInt(String.valueOf(colcharacter));
               if(columns[colindex-1] == 1)
               {
                  col_result = true;
                  break;
               }
               else
               {
                  columns[colindex-1] = 1; 
               }
            }
           
         
         }
         for (int q=0;q<9;q++)
         {
            rows[q] = 0;
            columns[q] = 0;
         }
      }
      String result = "";
           
      if(row_result) result = "INVALID ROW";   
      else if(col_result) result = "INVALID COLUMN"; 
      else if(region_result) result = "INVALID REGION";
      else if(hiphen) result = "OK SO FAR";
      else result = "VALID";
      
      return result;

   }
}
